package com.ciwong.epaper.widget;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.modules.evaluate.bean.ShareBean;
import com.ciwong.epaper.modules.share.QQShareHelper;
import com.ciwong.epaper.modules.share.QQShareListener;
import com.ciwong.epaper.modules.share.WxShareHelper;
import com.ciwong.epaper.modules.share.WxShareListener;
import com.ciwong.epaper.modules.share.bean.ShareMsgObj;
import com.ciwong.epaper.util.EConstants;
import com.ciwong.epaper.util.FileUtil;
import com.ciwong.mobilelib.utils.ToastUtil;
import com.tencent.tauth.UiError;

/**
 * 分享的dialog
 * <p>
 * Created by lzr on 2017/6/2.
 */
public class ShareQRcodeDialog extends Dialog implements View.OnClickListener {

    private View mWeiXinFr, mWeiXin;
    //            , mQQFr ,mQQSpace;

    private ShareBean shareBean;

    private int shareBitmapId;

    private String shareQQIocn;

    private String shareContent;

    private Activity mActivity;

    private ImageView share_qq;
    private ImageView share_qq_space;
    private Context mContext;

    public ShareQRcodeDialog(Context context) {
        super(context, R.style.read_choose_dialog);
        mContext = context;
        View view = LayoutInflater.from(context).inflate(R.layout.share_dialog, null);
        setContentView(view);
        mActivity = (Activity) context;
        initView();
        initEve();
        setCanceledOnTouchOutside(true);
        bindData();
        isSupportQQAndQQZone();

        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        layoutParams.width = context.getResources().getDisplayMetrics().widthPixels;
        layoutParams.height = (int) context.getResources().getDimension(R.dimen.share_dialog_hei);
        view.setLayoutParams(layoutParams);
        this.getWindow().setGravity(Gravity.BOTTOM);
    }


    private void bindData() {

        shareBean = new ShareBean();
        if (mContext != null) {
            shareBean.setUrl(mContext.getResources().getString(R.string.download_url));
            shareBean.setTitle(mContext.getResources().getString(R.string.app_name));
            shareContent = mContext.getResources().getString(R.string.download_tips);
        }
        shareBitmapId = R.drawable.share_ic_launcher;
        shareQQIocn = FileUtil.getShareAppIconLocalPath(getContext());


    }


    /**
     * 初始化控件
     */
    private void initView() {
        mWeiXinFr = findViewById(R.id.weixin_friend_lin);
        mWeiXin = findViewById(R.id.weixin_lin);
        //        mQQFr=findViewById(R.id.qq_share_lin);
        //        mQQSpace=findViewById(R.id.space_lin);
        share_qq = (ImageView) findViewById(R.id.share_qq);
        share_qq_space = (ImageView) findViewById(R.id.share_qq_space);

    }

    /**
     * 初始化事件
     */
    private void initEve() {
        mWeiXinFr.setOnClickListener(this);
        mWeiXin.setOnClickListener(this);
        share_qq.setOnClickListener(this);
        share_qq_space.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.weixin_lin) {
            weixinShare(1);
        } else if (v.getId() == R.id.weixin_friend_lin) {
            weixinShare(2);
        } else if (v.getId() == R.id.share_qq) {
            qqShare();
        } else if (v.getId() == R.id.share_qq_space) {
            qqZoneShare();
        }
        dismiss();
    }

    /**
     * @param type 1等于微信 2 等于朋友圈
     */
    private void weixinShare(int type) {
        try {
            WxShareHelper.getInstance().initWxShare(mActivity, new WxShareListener() {
                @Override
                public void onWxShareing(String str) {

                }

                @Override
                public void onWxShareSucc(String var) {
                    ToastUtil.INSTANCE.toastCenterSuccess( EApplication.getInstance().getResources().getString(R.string.share_success));
                }

                @Override
                public void onWxShareFailure(int var, String var1) {

                }

                @Override
                public boolean onWxShareCancel() {

                    return false;
                }
            });
            ShareMsgObj shareMsgObj = new ShareMsgObj();
            shareMsgObj.setWxType(type);
            shareMsgObj.setShareType(ShareMsgObj.ShareType.SHARE_WEB);
            Bitmap bmp = BitmapFactory.decodeResource(this.getContext().getResources(), shareBitmapId);
            shareMsgObj.setThunbBmp(bmp);
            shareMsgObj.setUrl(shareBean.getUrl());
            switch (type) {
                case 1:
                    shareMsgObj.setMsgTitle(shareBean.getTitle());
                    shareMsgObj.setMsgDesription(shareContent);
                    break;
                case 2:
                    shareMsgObj.setMsgTitle(shareContent);
                    break;
            }
            WxShareHelper.getInstance().sendReq(shareMsgObj);
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("TAG", "weixinShare: " + e.getMessage());
        }

    }

    /**
     * qq空间分享
     */
    private void qqZoneShare() {
        try {
            ShareMsgObj shareMsgObj = new ShareMsgObj();
            shareMsgObj.setShareType(ShareMsgObj.ShareType.SHARE_WEB);
            shareMsgObj.setImg_url(shareQQIocn);
            shareMsgObj.setUrl(shareBean.getUrl());
            shareMsgObj.setMsgTitle(shareContent);
            QQShareHelper.getInstance().shareToQZone(mActivity, shareMsgObj, new QQShareListener() {

                @Override
                public void onQQShareSucc(Object var) {

                }

                @Override
                public void onQQShareFailure(UiError var1) {
                    ToastUtil.INSTANCE.toastCenterSuccess(EApplication.getInstance().getResources().getString(R.string.share_success));
                }

                @Override
                public boolean onQQShareCancel() {
                    return false;
                }
            });
        } catch (Exception e) {
            e.getStackTrace();
        }
    }

    /**
     * QQ分享
     */
    private void qqShare() {
        try {
            ShareMsgObj shareMsgObj = new ShareMsgObj();
            shareMsgObj.setShareType(ShareMsgObj.ShareType.SHARE_WEB);
            shareMsgObj.setMsgTitle(shareBean.getTitle());
            shareMsgObj.setLocalImgUrl(shareQQIocn);
            shareMsgObj.setUrl(shareBean.getUrl());
            shareMsgObj.setMsgDesription(shareContent);
            QQShareHelper.getInstance().shareToQQ(mActivity, shareMsgObj, new QQShareListener() {
                @Override
                public void onQQShareSucc(Object var) {
                    ToastUtil.INSTANCE.toastCenterSuccess( EApplication.getInstance().getResources().getString(R.string.share_success));
                }

                @Override
                public void onQQShareFailure(UiError var1) {
                }

                @Override
                public boolean onQQShareCancel() {
                    return false;
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void isSupportQQAndQQZone() {
        if (EConstants.QQ_APP_ID.equals("") || null == EConstants.QQ_APP_ID) {
            share_qq.setImageResource(R.mipmap.share_qq_unstalled);
            share_qq_space.setImageResource(R.mipmap.share_qq_space_unstalled);
            share_qq.setEnabled(false);
            share_qq_space.setEnabled(false);
        }
    }

}
